/**
 * RunningTableModel.java
 * @author: liuex
 * 2007-12-31 ����03:37:19
 *
 * note: 
 */
package lex.mtd.ui.download;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import org.lex.utils.text.SizeFormat;

import lex.mtd.core.DownloadManager;

public class DownloadTableModel extends AbstractTableModel
{
	private static final String[] COLUMN_NAMES = { "Name", "Size", "Transferred", "Progress",
			"Speed", "Thread", "URLs" };
	private ArrayList<DownloadManager> data = null;

	public DownloadTableModel()
	{
		data = new ArrayList<DownloadManager>();
	}

	public void addDownloadManager( DownloadManager dm )
	{
		this.data.add( dm );
		this.fireTableDataChanged();
	}

	public DownloadManager getDownloadManager( int modelRowIndex )
	{
		return data.get( modelRowIndex );
	}

	@Override
	public String getColumnName( int column )
	{
		return COLUMN_NAMES[column];
	}

	public int getColumnCount()
	{
		return COLUMN_NAMES.length;
	}

	public int getRowCount()
	{
		return data.size();
	}

	public Object getValueAt( int row, int column )
	{
		DownloadManager dm = data.get( row );
		switch( column )
		{
		case 0:
			// name
			return dm.getFileName();
		case 1:
			// size
			return SizeFormat.autoFormat( dm.getContentLength() );
		case 2:
			// transferred
			return SizeFormat.autoFormat( dm.getFileSet().getTransferredBytes() );
		case 3:
			// progress
			return dm.getFileSet().getTransferredBytes() / ((double) dm.getContentLength());
		case 4:
			// speed
			return SizeFormat.autoFormat( dm.getThreadSet().getAverageSpeed() );
		case 5:
			// thread count
			return dm.getThreadSet().getThreadCount();
		case 6:
			// urls
			return dm.getDownloadTask().getURLSet().toString( SEP );
		}
		return "";
	}

	private static final String SEP = ", ";
}
